# import time
from pref import prefs
from pyo import *
import OscServer
# from AudioFx import modulesDict

### .stop() sut Gate et Compress si a off...

class AudioInStrip(object):
    ### audioIn en premier argument sans valeur par defaut?
    def __init__(self, addressPrepend = "/AudioInStrip", audioIn = Sig([0 for i in range(prefs["channels"])])):
        
        self.osc = OscServer.OscCtrls(addressPrepend)
        self.volume = self.osc.newAudio("volume",[0],"/audio","/volume/x","lemur1",[0])
        self.compThresh = self.osc.newAudio("compThresh",[1],"/audio","/compThresh/x","lemur1",[0])
        self.compRatio = self.osc.newAudio("compRatio",[2],"/audio","/compRatio/x","lemur1",[0])
        self.compGain = self.osc.newAudio("compGain",[3],"/audio","/compGain/x","lemur1",[0])
        self.gateThresh = self.osc.newAudio("gateThresh",[4],"/audio","/gateThresh/x","lemur1",[0])
        self.audioIn = Sig(audioIn, mul = self.volume**2)
        self.osc.newData("mute","i",self.mute, "/mute/x","/mute/x", "lemur1")
        self.osc.newData("gateOn","i",self.gateOn,"/gateOn/x","/gateOn/x", "lemur1")
        self.osc.newData("compOn","i",self.compOn,"/compOn/x","/compOn/x", "lemur1")
        self.osc.send("volume", 0)
        self.osc.send("gateThresh", 0)
        self.osc.send("compThresh", 0)
        self.osc.send("compRatio", 0)   
        self.osc.send("compGain", 0)
        self.osc.send("mute", 1)
        self.osc.send("gateOn", 0)
        self.osc.send("compOn", 0)
        self.gate = Gate(self.audioIn, thresh = AToDB(self.gateThresh**5)).stop()
        self.toComp = Sig(self.audioIn)
        self.comp = Compress(self.toComp, thresh = AToDB(self.compThresh), ratio = self.compRatio*19+1, mul = self.compGain*3+1).stop()
        self.output = Sig(self.toComp)
    
    def getAudio(self):
        return self.output

    def mute(self, val):
        print "mute", val
        if val:
            self.audioIn.setMul(self.volume**2)
        else:
            self.audioIn.setMul(0)

    def gateOn(self, val):
        if val:
            self.gate.play()
            self.toComp.setValue(self.gate)
        else:
            self.gate.stop()
            self.toComp.setValue(self.audioIn)


    def compOn(self, val):
        if val:
            self.comp.play()
            self.output.setValue(self.comp)
        else:
            self.comp.stop()
            self.output.setValue(self.toComp)